package cn.schoolwow.quickdao.test.module.business.user.dml.json.testcase;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.test.module.business.user.dml.user.flow.delete.CheckDeleteUserResultFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;

public class DeleteUserJSONTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        deleteUserBySingleUniqueFieldName(flowContext);
        deleteUserByMultipleUniqueFieldName(flowContext);
    }

    @Override
    public String name() {
        return "根据指定唯一字段列表删除用户测试用例-JSON";
    }

    private void deleteUserBySingleUniqueFieldName(FlowContext flowContext){
        flowContext.executeFlowList(new AddUserJSONTestCaseFlow());
        {
            DAO dao = flowContext.checkInstanceData(DAO.class);
            JSONObject insertUser = flowContext.checkData("insertUser", JSONObject.class);

            int effect = dao.uniqueFieldNames("id").delete("user", insertUser);
            Assert.assertEquals(1, effect);
        }
        {
            DAO dao = flowContext.checkInstanceData(DAO.class);
            JSONArray insertUserList = flowContext.checkData("insertUserList", JSONArray.class);

            int effect = dao.uniqueFieldNames("id").delete("user", insertUserList);
            Assert.assertEquals(insertUserList.size(), effect);
        }
        flowContext.executeFlowList(new CheckDeleteUserResultFlow());
    }

    private void deleteUserByMultipleUniqueFieldName(FlowContext flowContext){
        flowContext.executeFlowList(new AddUserJSONTestCaseFlow());
        {
            DAO dao = flowContext.checkInstanceData(DAO.class);
            JSONObject insertUser = flowContext.checkData("insertUser", JSONObject.class);

            int effect = dao.uniqueFieldNames("username", "password").delete("user", insertUser);
            Assert.assertEquals(1, effect);
        }
        {
            DAO dao = flowContext.checkInstanceData(DAO.class);
            JSONArray insertUserList = flowContext.checkData("insertUserList", JSONArray.class);

            int effect = dao.uniqueFieldNames("username", "password").delete("user", insertUserList);
            Assert.assertEquals(insertUserList.size(), effect);
        }
        flowContext.executeFlowList(new CheckDeleteUserResultFlow());
    }
}
